package record.wilson.flutter.com.flutter_plugin_record;

import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationChannelCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.graphics.drawable.IconCompat;

import record.wilson.flutter.com.flutter_plugin_record.R;

/**
 * 前台service调整   我要让应用在后台 也能够监听麦克风
 */
public class RecordService extends Service {
    String channelID = "YILIAN_RECORD";
    String channelName = "易练录音中";
    String channelDescription = "为保障服务质量，正在录音";
    private static final String TAG = "NotificationService";
    private NotificationManager notificationManager;
    //通知的标识号。
    private int NOTIFICATION = 999;
    public static RecordService service;//启动service时候我们记录这service方便移除通知

    @Override
    public void onCreate() {
        super.onCreate();
        initChannel();
        service = this;
        showNotification();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    public void showNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelID);
        builder.setSmallIcon(R.drawable.ic_record)
                .setContentTitle(channelName)
                .setContentText(channelDescription)
                .setSound(null)//车震 声音啥的关了
                .setVibrate(null)
                .setOngoing(true)
                .build();
        startForeground(NOTIFICATION, builder.build());
    }
    private void initChannel() {
        NotificationChannelCompat notificationChannelCompat = new NotificationChannelCompat
                .Builder(channelID, NotificationManagerCompat.IMPORTANCE_HIGH)
                .setName(channelName)
                .setDescription(channelDescription)
                .setLightsEnabled(true)
                .setLightColor(Color.GREEN)
                //角标
                .setShowBadge(false)
                //true 震动开启，false 震动关闭
                .setVibrationEnabled(true)
                .build();
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
        managerCompat.createNotificationChannel(notificationChannelCompat);
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 清除通知
     */
    public void onClearNotify() {
        stopForeground(true);
    }
}